<template>
    <div>
        <HomeView></HomeView>
        <div class="news">
            <el-button type="primary" plain @click="dialog1Visible = true">新增</el-button>
                <el-table
                    :data="table"
                    style="width: 100%">
                    <el-table-column

                        prop="newsKey"
                        label="新闻编号"
                        width="180">
                    </el-table-column>
                    <el-table-column
                    
                        prop="title"
                        label="新闻标题"
                        width="300">
                    </el-table-column>
                    <el-table-column
                    
                        prop="date"
                        label="日期">
                    </el-table-column>
                    <el-table-column
                        prop="roleKey"
                        label="操作人编号">
                    </el-table-column>
                    <el-table-column
                        fixed="right"
                        label="操作"
                        width="100">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 新增的表单 -->
                <el-dialog title="新闻信息" :visible.sync="dialog1Visible" width="30%">
                    <el-form ref="form" :model="table" label-width="80px">
                        <el-form-item label="新闻标题">
                        <el-input v-model="table.title"></el-input>
                        </el-form-item>
                        <el-form-item label="内容">
                        <el-input
                            type="textarea"
                            :row="2"
                            v-model="table.content">
                        </el-input>
                        </el-form-item>
                    </el-form>
                    <span slot="footer" class="dialog-footer">
                        <el-button @click="dialog1Visible = false">取 消</el-button>
                        <el-button type="primary" @click="add">确 定</el-button>
                    </span>
                    </el-dialog>
            </div>
    </div>
</template>

<script>
import HomeView from './HomeView.vue';
export default{
    data(){
        return{
            dialog1Visible:false,
            table:[]
        }
    },

    components:{
        HomeView
    },
    created(){
    
            this.$axios.post('/news/newsList',{},{
            headers:{
                "token":JSON.parse(localStorage.getItem('user')).token
            } 
            })
            .then(response =>{
                    this.form = response.data.data;
                    this.table=this.form
                    for(let i=0;i<this.table.length;i++){
                        this.table[i].id=i
                    }
                })
    },
    methods:{
        add(){
            this.dialog1Visible = true
                this.$axios.post('/news/addNews',{
                    title:this.table.title,
                    content:this.table.content,
                    roleKey:JSON.parse(localStorage.getItem('user')).roleKey
                },{
                    headers:{
                        "token":JSON.parse(localStorage.getItem('user')).token
                    }
                })
                .then(res=>{
                    if(res.data.msg=='success'){
                        alert("新增成功")
                    }
                    else{
                        alert("新增失败")
                    }
                })
                this.dialog1Visible=false
                this.$router.go(0)              
            },
            del(id){
                var news=this.table[id]
                this.$axios.post('/news/deleteNews',{
                    newsKey:news.newsKey
                },{
                    headers:{
                        "token":JSON.parse(localStorage.getItem('user')).token
                    } 
                })
                .then(res=>{
                    if(res.data.msg=="success"){
                        alert("删除成功咯")
                        this.$router.go(0)  
                    }else {
                        alert("删除失败")
                    }
                })
            }

        }
}
</script>